import {Component, OnInit, ViewEncapsulation, Input, OnDestroy} from '@angular/core';
import {Router} from "@angular/router";
import {OrderService} from "@order/order.service";
import {ToolsService} from "@shared/service/tools/tools.service";

import {Subject} from "rxjs";
import {OrderDetailRespVO} from "@interface/index";


@Component({
  selector: 'seed-order-item',
  templateUrl: 'order-item.component.html',
  styleUrls: ['order-item.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class OrderItemComponent implements OnInit , OnDestroy {
  private orderNumber: string;
  public show = false;
  public orderInfo: OrderDetailRespVO = new OrderDetailRespVO();
  public ngUnsubscribe: Subject<void> = new Subject<void>();

  @Input() orderList;
  constructor(
    public router: Router,
    public orderService: OrderService,
    public tools: ToolsService
  ) {}

  /**
   * 跳转订单详情页
   * @param
   */
  goToOrderDetail(orderId: number) {
    orderId && this.router.navigate(['/order/detail', orderId]);
  }

  getDetailInfo(orderNumber: string) {
    this.orderService.getOrdersDetail(orderNumber)
      .takeUntil(this.ngUnsubscribe)
      .subscribe(res => {
        this.goToSignUp(res.data);
      });
  }
  /**
   * 跳转订支付页
   * @param orderId ：订单id
  * @author @chensk740
   * @date 2017-11-30
   */
  getToPay(orderId: number) {
/**    orderId && this.router.navigate(['/payment/pay', orderId]);*/
    orderId && this.router.navigate(['/payment/pay'], {
      queryParams: { orderNumber: orderId }
    });
  }
  /**
   * 跳转报名页面
   * @param orderInfo ：订单信息
   * @author zhupw1360
   * @date 2017-11-30
   */
  goToSignUp(orderInfo) {
    const goodsOne = orderInfo.details[0];
    const params = {
      goodsCode: orderInfo.details[0].goodsCode,
      goodsCateCode1: goodsOne.goodsCateCode1,
      goodsCateCode: goodsOne.goodsCateCode,
      skuCode: goodsOne.skuCode,
      orderNumber: orderInfo.orderNumber
    };
    this.orderService.getGoodsTemplateUrl(params);
  }

  ngOnInit() {
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

}
